package com.lavegue.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.lavegue.common.BaseHibernateDao;
import com.lavegue.common.Page;
import com.lavegue.dao.TopUpDao;
import com.lavegue.model.Msg;
import com.lavegue.model.TopUp;

public class TopUpDaoImpl extends BaseHibernateDao<TopUp, Integer> implements TopUpDao{

	public TopUpDaoImpl() {
		super(TopUp.class);
		// TODO Auto-generated constructor stub
	}

	/**ljp 20111229
	 * 查询用户的所有充值记录
	 * @param pageNo    当前页数
	 * @param pageSize  每页多少条
	 * @param beginTime 选择开始时间
	 * @param endTime   选择结束时间
	 * @param senderUid  发送者的用户ID
	 * @return
	 */
	public Page<TopUp> findMsgRechargeRecordList(int pageNo,int pageSize,Date beginTime,Date endTime,int senderUid)throws Exception{
		List<Criterion> condition = new ArrayList<Criterion>();
		//条件查询	
		// beginTime< sendTime <endTime		
		if(null != beginTime && null != endTime){
			condition.add(Expression.between("topupTime", beginTime, new Date(endTime.getTime()+1000*60*60*24)));
		}else{
			if(null != beginTime){  //选择开始时间
				//查询某一天的记录[sendTime >= beginTime and sendTime < beginTime+一天的时间]
				condition.add(Expression.and(Expression.ge("topupTime", new java.sql.Date(beginTime.getTime())),Expression.lt("topupTime", new java.sql.Date(beginTime.getTime()+1000*60*60*24))));
			}
			if(null != endTime){   //选择结束时间
				  condition.add(Restrictions.and(Expression.ge("topupTime", new java.sql.Date(endTime.getTime())),Expression.lt("topupTime", new java.sql.Date(endTime.getTime()+1000*60*60*24))));
			}
		}
		
		if(senderUid != 0){  //发送者的用户ID
			condition.add(Restrictions.eq("userInfo.id", Integer.valueOf(senderUid)));
		}
		//查询列表,通过时间排序
		Page<TopUp> page = this.findPage(pageNo, pageSize, condition, Order.desc("topupTime"));
		return page;
	}

	public List<TopUp> findByUser(int userid) {
		List<Criterion> condition = new ArrayList<Criterion>();
		condition.add(Restrictions.eq("userInfo.id", userid));
		condition.add(Restrictions.gt("remainNum", 0));
		Page<TopUp> page = this.findPage(1, 10000, condition, Order.asc("topupTime"));
		return page.getData();
	}
	
}
